package com.code.user.model.po;

import com.code.common.annotations.EnumValue;
import com.code.common.models.po.BasePO;
import com.code.common.validators.CreateGroup;
import com.code.common.validators.UpdateGroup;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.hibernate.validator.constraints.Length;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 登录凭证持久层对象
 *
 * @author tangyifei
 */
@ApiModel("登录凭证PO")
@EqualsAndHashCode(callSuper = false)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LoginCredential extends BasePO<Long> {

    private static final long serialVersionUID = 5550420394013305835L;

    @ApiModelProperty(value = "主键 添加时非必填 修改、删除时必填", example = "1")
    @Id
    @GeneratedValue(generator = "JDBC")
    private Long id;

    @ApiModelProperty(value = "用户主键", example = "1")
    @NotNull(message = "用户主键不能为空", groups = {CreateGroup.class, UpdateGroup.class})
    private Long userId;

    @ApiModelProperty(value = "用户账号", example = "1")
    @NotBlank(message = "用户账号不能为空")
    @Length(min = 1, max = 30)
    private String account;

    @ApiModelProperty(value = "用户密码（MD5加密传输）", example = "35ecd85942c861d34da6bcb972f9594125608b94a995f253c7eae228aa7b687c")
    @NotBlank(message = "用户密码（MD5加密传输）不能为空")
    private String pwd;

    @ApiModelProperty(value = "密码加密随机盐", example = "1")
    @Length(min = 1, max = 64)
    private String randomSalt;

    @ApiModelProperty(value = "账号类型", example = "1")
    @EnumValue(enumClass = LoginCredential.LoginCredentialTypeEnum.class, enumMethod = "isValidName")
    private String accountType;

    /**
     * 账号类型枚举
     */
    public enum LoginCredentialTypeEnum {

        /**
         * 自定义
         */
        CUSTOM,

        /**
         * 微信UNION ID
         */
        UNION_ID,

        /**
         * 微信OPEN ID
         */
        OPEN_ID;

        public static boolean isValidName(String name) {
            for (LoginCredential.LoginCredentialTypeEnum typeEnum : LoginCredential.LoginCredentialTypeEnum.values()) {
                if (typeEnum.name().equals(name)) {
                    return true;
                }
            }
            return false;
        }
    }
}
